package com.lanqiao.gxnushop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.gxnushop.entity.Reviews;
import com.lanqiao.gxnushop.service.ReviewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log4j2
@Tag(name = "ReviewsController", description = "评价管理")
@RestController
public class ReviewsController {
    @Autowired
    private ReviewsService reviewsService;

    @PostMapping("/reviews")
    @Operation(summary = "增加评价")
    @Parameter(name = "reviews", description = "评价reviewsId(自动生成),buyerId(买家Id),comment(评价内容),date(自动生成),orderId(订单Id),productId(商品Id),rating(评分)")
    public boolean addReviews(Reviews reviews) {
        return reviewsService.add(reviews);
    }

    @DeleteMapping("/reviews/{reviewsId}")
    @Operation(summary = "根据指定reviewsId删除评价")
    @Parameter(name = "reviewsId", description = "评价reviewsId")
    public boolean deleteReviews(@PathVariable String reviewsId) {
        return reviewsService.delete(reviewsId);
    }

    @GetMapping("/reviews/select/{buyerId}")
    @Operation(summary = "根据买家id查询评价 买家的所有评价")
    @Parameter(name = "buyerId", description = "买家buyerId")
    public PageInfo<Reviews> findByBuyerId(@PathVariable String buyerId, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Reviews> list = reviewsService.findByBuyerId(buyerId);
        return new PageInfo<>(list);
    }

    @GetMapping("/reviews/{reviewsId}")
    @Operation(summary = "根据评价reviewsId查询指定评价")
    @Parameter(name = "reviewsId", description = "评价reviewsId")
    public Reviews findById(@PathVariable String reviewsId) {
        return reviewsService.selectByReviewId(reviewsId);
    }

    @PutMapping("/reviews")
    @Operation(summary = "根据评价reviewsId更新评价")
    @Parameter(name = "reviews", description = "评价reviewsId")
    public boolean updateReviews(Reviews reviews) {
        return reviewsService.update(reviews);
    }

    @GetMapping("/reviews/selectAll/{productId}")
    @Operation(summary = "根据商品Id查找该商品的所有评价")
    @Parameter(name = "productId", description = "商品productId")
    public PageInfo<Reviews> findByProductId(@PathVariable String productId, Page pageable) {
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Reviews> list = reviewsService.selectByProductId(productId);
        return new PageInfo<>(list);
    }
}
